package mapEditor.vo
{
	import flash.utils.Dictionary;
	
	import mapEditor.data.AssetTemplateInfo;
	
	import utils.DisplayUtil;

	public class AssetGroupVO
	{
		private var _path:String;
		private var _xml:XML;
		private var _items:Dictionary=new Dictionary(true);
		
		public function AssetGroupVO(xml:XML=null)
		{
			_xml=xml;
			if(_xml!=null)
			{
				_path=_xml.@path;
				for each(var itemXml:XML in _xml.item)
				{
					var assetVO:AssetVO=new AssetVO();
					assetVO.className=itemXml.@className;
					assetVO.id=itemXml.@id;
					assetVO.keywords=itemXml.@keywords;
					assetVO.name=itemXml.@name;
					assetVO.registerX=int(itemXml.@registerX);
					assetVO.registerY=int(itemXml.@registerY);
					assetVO.registerZ=int(itemXml.@registerZ);
					_items[assetVO.id]=assetVO;
					
					AssetTemplateInfo.instance.maxAssetID=assetVO.id>=AssetTemplateInfo.instance.maxAssetID ? assetVO.id : AssetTemplateInfo.instance.maxAssetID;
				}
			}
		}

		public function get path():String
		{
			return _path;
		}

		public function set path(value:String):void
		{
			_path = value;
		}
		
		/**
		 *增加一个资源项
		 * @param assetVO 资源项
		 * 
		 */		
		public function addAssetVO(assetVO:AssetVO):void
		{
			if(_items[assetVO.id]!=null)return;
			_items[assetVO.id]=assetVO;
		}
		
		/**
		 *移除一个资源项
		 * @param assetVO 资源项
		 * 
		 */		
		public function removeAssetVO(assetVO:AssetVO):void
		{
			if(_items.indexOf(assetVO)==-1)return;
			_items.splice(_items.indexOf(assetVO), 1);
		}		
		
		/**
		 *根据ID取一个资源项 
		 * @param id 资源项ID
		 * @return 
		 * 
		 */		
		public function getAssetVOByID(id:int):AssetVO
		{
			return _items[id] as AssetVO;
		}
		
		public function dispose():void
		{
			DisplayUtil.dispose(_items);
			_items=null;
			_xml=null;
		}

		public function get items():Dictionary
		{
			return _items;
		}
	}
}